/************************************************************************
Purpose: Plot Permit Prices and Quantities Time Series 	
*************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_CLEAN/panel_plant-period-date-bid.dta", clear
drop if bid_status == "REJ"
drop if missing(bid_qty)
format date %tddd-Mon-YY

** Toggles
local price = 1
local quantity = 1

********************************************************************************
*** Price 
********************************************************************************

if `price' == 1 {

	preserve
	
	** drop outlier
	drop if date == td(08feb2021) & commodity == "PSUM010121"
	
	bysort period date: egen trade_price_day = mean(trade_price)
	bysort period date: egen bid_price_day = mean(bid_price)
	keep date period trade_price_day bid_price_day is_auction
	sort date period
	duplicates drop

	local xtext_pos = 51	
	local x_opt ///
		xtick(21746 21777 21808 21838 21869 21899 21930 21961 21990 22021 22051 22082 22112 22143 22174 22204 22235 22265 22296 22327 22355 22386) ///
		xlabel(21746 21838 21930 22021 22112 22204 22296 22386, angle(30) labsize(small)) ///
		xline(21745, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21746 "Mock-I", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21774, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21775 "Mock-II", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21808, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21809 "Period-I", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21823, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(21838, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21839 "Period-II", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21853.5, lwidth(5.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21869, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21870 "Period-III", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21892, lwidth(8.2) lcolor(gs14%30) lpattern(solid)) ///
		xline(21915, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21916 "Period-IV", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21930.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(21946, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21947 "Period-V", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21960.5, lwidth(5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21975, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21976 "Period-VI", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21985.5, lwidth(3.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21996, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21997 "Interregnum", place(se) orientation(vertical) size(vsmall) color(edkblue%70)) ///
		xline(22098, lwidth(37.5) lcolor(ebg%50) lpattern(solid)) ///
		xline(22200, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22201 "Mock-III", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22231, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22232 "Interregnum", place(se) orientation(vertical) size(vsmall) color(edkblue%70)) ///
		xline(22240.5, lwidth(3.4) lcolor(ebg%50) lpattern(solid)) ///
		xline(22250, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22251 "Period-VII", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22265.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(22281, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22282 "Period-VIII", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22296.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(22312, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22313 "Period-IX", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22326, lwidth(5) lcolor(gs14%30) lpattern(solid)) ///
		xline(22340, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22341 "Period-X", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22355.5, lwidth(5.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(22371, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		xline(22380, lwidth(3) lcolor(gs14%30) lpattern(solid)) 

	twoway ///
		line trade_price_day date if period == 1, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line trade_price_day date if period == 2, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line trade_price_day date if period == 3, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line trade_price_day date if period == 4, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line trade_price_day date if period == 5, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line trade_price_day date if period == 6 & date <= td(22mar2020), ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line trade_price_day date if period == 7, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line trade_price_day date if period == 8, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line trade_price_day date if period == 9, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line trade_price_day date if period == 10, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		scatter bid_price_day date if period == 1 & is_auction == 1, msize(vsmall) mcolor(midblue) || ///
		scatter bid_price_day date if period == 2 & is_auction == 1, msize(vsmall) mcolor(black) || ///
		scatter bid_price_day date if period == 3 & is_auction == 1, msize(vsmall) mcolor(midblue) || ///
		scatter bid_price_day date if period == 4 & is_auction == 1, msize(vsmall) mcolor(black) || ///
		scatter bid_price_day date if period == 5 & is_auction == 1, msize(vsmall) mcolor(midblue) || ///
		scatter bid_price_day date if period == 6 & is_auction == 1 & date <= td(22mar2020), msize(vsmall) mcolor(black)  || ///
		scatter bid_price_day date if period == 7 & is_auction == 1, msize(vsmall) mcolor(midblue) || ///
		scatter bid_price_day date if period == 8 & is_auction == 1, msize(vsmall) mcolor(black)  || ///
		scatter bid_price_day date if period == 9 & is_auction == 1, msize(vsmall) mcolor(midblue) || ///
		scatter bid_price_day date if period == 10 & is_auction == 1, msize(vsmall) mcolor(black)  || ///
		pci 4.825 21808 4.825 21996, lcolor(red) lwidth(vthin) lpattern(dash) || ///
		pci 4.825 22250 4.825 22380, lcolor(red) lwidth(vthin) lpattern(dash) ///
		plotregion(color(white)) bgcolor(white) graphregion(color(white)) ///
		xtitle("") ///
		ytitle("Permit price (Rs/kg)", size(small)) ///
		ylabel(0(5)50, labsize(small) angle(horizontal)) ///
		xsize(14) ysize(10) ///
		legend(cols(3) size(small) region(lstyle(none)) position(6)) ///
		legend(on order(1 "Market-clearing price" 11 "Bid price" 21 "Price floor")) ///
		`x_opt'

	graph export "$TRADING_FIGS/Figure_2_A.pdf", replace

	restore
}


********************************************************************************
*** Quantity
********************************************************************************

if `quantity' == 1{

	preserve
	
	gen purchase_qty = trade_qty
	replace purchase_qty = 0 if missing(purchase_qty) | purchase_qty < 0
	bysort date period: egen purchase_qty_day = sum(purchase_qty)
	gen purchase_qty_day_per_cap = purchase_qty_day / compliance_cap * 100
	keep date period purchase_qty_day_per_cap
	sort date period

	local xtext_pos = 25.5
	local x_opt xtick(21746 21777 21808 21838 21869 21899 21930 21961 21990 22021 22051 22082 22112 22143 22174 22204 22235 22265 22296 22327 22355 22386) ///
		xlabel(21746 21838 21930 22021 22112 22204 22296 22386, angle(30) labsize(small)) ///
		xline(21745, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21746 "Mock-I", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21774, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21775 "Mock-II", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21808, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21809 "Period-I", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21823, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(21838, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21839 "Period-II", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21853.5, lwidth(5.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21869, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21870 "Period-III", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21892, lwidth(8.2) lcolor(gs14%30) lpattern(solid)) ///
		xline(21915, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21916 "Period-IV", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21930.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(21946, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21947 "Period-V", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21960.5, lwidth(5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21975, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21976 "Period-VI", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(21985.5, lwidth(3.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(21996, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 21997 "Interregnum", place(se) orientation(vertical) size(vsmall) color(edkblue%70)) ///
		xline(22098, lwidth(37.5) lcolor(ebg%50) lpattern(solid)) ///
		xline(22200, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22201 "Mock-III", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22231, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22232 "Interregnum", place(se) orientation(vertical) size(vsmall) color(edkblue%70)) ///
		xline(22240.5, lwidth(3.4) lcolor(ebg%50) lpattern(solid)) ///
		xline(22250, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22251 "Period-VII", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22265.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(22281, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22282 "Period-VIII", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22296.5, lwidth(5.4) lcolor(gs14%30) lpattern(solid)) ///
		xline(22312, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22313 "Period-IX", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22326, lwidth(5) lcolor(gs14%30) lpattern(solid)) ///
		xline(22340, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		text(`xtext_pos' 22341 "Period-X", place(se) orientation(vertical) size(vsmall) color(gs8)) ///
		xline(22355.5, lwidth(5.5) lcolor(gs14%30) lpattern(solid)) ///
		xline(22371, lwidth(vthin) lcolor(black) lpattern(tight_dot)) ///
		xline(22380, lwidth(3) lcolor(gs14%30) lpattern(solid)) 

	twoway ///
		line purchase_qty_day_per_cap date if period == 1, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 2, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 3, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 4, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 5, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 6 & date <= td(22mar2020), ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 7, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 8, ///
		clwidth(thin) clcolor(black) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 9, ///
		clwidth(thin) clcolor(midblue) mcolor(midblue) || ///
		line purchase_qty_day_per_cap date if period == 10, ///
		clwidth(thin) clcolor(black) mcolor(midblue) ///	
		plotregion(color(white)) bgcolor(white) graphregion(color(white)) ///
		xtitle("") ///
		ytitle("Permits purchased / period cap (%)", size(small)) ///
		ylabel(0(5)25, labsize(small) angle(horizontal)) ///
		xsize(14) ysize(10) aspect(0.4) ///
		legend(cols(1) size(small) region(lstyle(none))) ///
		legend(off order(1 "Trade Price" 11 "Average Order Price")) ///
		`x_opt'

	graph export "$TRADING_FIGS/Figure_2_B.pdf", replace

	restore

}
